package com.tjh;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author tang'jia'hao
 * @description :
 * @package com.tjh(jdbc_1)
 * @date 2021-09-12 13:59
 */
public class ConnectionTest{

    // 方式一
    @Test
    public void testConnection1() throws SQLException {

        // 获取Driver的实现类对象
        Driver driver = new com.mysql.jdbc.Driver();
        
        String url = "jdbc:mysql://localhost:3306/test";
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "tjh20010228");

        Connection conn = driver.connect(url, info);

        System.out.println("conn = " + conn);
    }

    // 方式二：对方式一的迭代
    @Test
    public void testConnection2() throws ClassNotFoundException, NoSuchMethodException,
            InvocationTargetException, InstantiationException, IllegalAccessException, 
            SQLException {
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) aClass.getDeclaredConstructor().newInstance();
        // Driver driver = (Driver) aClass.newInstance();
        
        String url = "jdbc:mysql://localhost:3306/test";
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "tjh20010228");

        Connection conn = driver.connect(url, info);
        System.out.println("conn = " + conn);
    }
    
    // 方式三:使用DriverManager替换Driver
    @Test
    public void testConnection3() throws Exception {
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) aClass.getDeclaredConstructor().newInstance();
        
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "tjh20010228";
        
        // 注册驱动
        DriverManager.registerDriver(driver);
        
        // 获取连接
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println("conn = " + conn);
    }

    // 方式四:优化方法三
    @Test
    public void testConnection4() throws Exception {
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "tjh20010228";

        // 以下也可以省略,在mysql中可以,oracle不可以
        Class.forName("com.mysql.jdbc.Driver");
        // 源码static{}语句块类加载的时候自动执行

        // 获取连接
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println("conn = " + conn);
    }

    // 方式五:最终版本,使用配置文件
    @Test
    public void getConnection5() throws IOException, ClassNotFoundException, SQLException {

        // 读取配置文件
        InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties"); // 类加载器

        Properties pros = new Properties();
        pros.load(is);

        String user = pros.getProperty("user");
        String password = pros.getProperty("password");
        String url = pros.getProperty("url");
        String driverClass = pros.getProperty("driverClass");
        
        Class.forName(driverClass);

        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println("conn = " + conn);
    }
}

